from collections import namedtuple
import csv
import os
import paste

NewRaceDataRecord = namedtuple('NewRaceDataRecord', 'yr,unit,school,grade,total,wht_tot,blk_tot,nam_tot,api_tot,hsp_tot,mlt_tot,asn_tot,hpi_tot,mis_tot')
OldRaceDataRecord = namedtuple('OldRaceDataRecord', 'yr,unit,school,grade,total,wht_tot,white_pct,blk_tot,black_pct,nam_tot,ant_amer_pct,asn_tot,asian_pct,hsp_tot,hispanic_pct,mlt_tot,multi_race_pct,mexican_total,mexcian_pct,puerto_rican_total,puerto_rican_pct,cuban_total,cuban_pct,other_hispanic_total,other_hispanic_pct,multi_hispanic_total,multi_hispanic_pct')


def new_race_data_records(filename):
    for record in map(RaceDataRecord._make, csv.reader(open(filename, "rb"))):
        yield record


def old_race_data_records(filename):
    for record in map(OldRaceDataRecord._make, csv.reader(open(filename, "rb"))):
        yield record


if __name__ == "__main__":
    from pyramid.paster import bootstrap
    env = bootstrap('../development.ini')
    

    from cpswarehouse.models import DBSession, YearDimension, GradeDimension, RacialFact, SchoolDimension
    import transaction

    session = DBSession()
    #data_filename = "cps_racial_data.csv"    

    #should take this from arguments
    #records = race_data_records(data_filename)
    records = old_race_data_records("race_survey_raw_data.csv")
    records.next() #advance past header


    for record in records:

        year = YearDimension.get_instance(record.yr)
        grade = GradeDimension.get_instance(record.grade)
        school = SchoolDimension.get_instance(record.school, record.unit)
        
        session.add(RacialFact(year,
                               grade,
                               school,
                               nativeamerican=record.nam_tot,
                               asian=record.asn_tot,
                               black=record.blk_tot,
                               hispanic=record.hsp_tot,
                               multiracial=record.mlt_tot,
                               white=record.wht_tot))


    print "Imported %s YearDimensions." % len(YearDimension.items)
    for year in YearDimension.items.values():
        session.add(year)

    print "Imported %s GradeDimensions." % len(GradeDimension.items)
    for grade in GradeDimension.items.values():
        session.add(grade)

    print "Imported %s SchoolDimensions." % len(SchoolDimension.items)
    for school in SchoolDimension.items.values():
        session.add(school)
    transaction.commit()
